package com.deshan.websys.api.controller;

import java.util.List;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.deshan.websys.api.utils.CommonResult;
import com.deshan.websys.code.model.SocialInfo;
import com.deshan.websys.code.service.SocialInfoService;

import cn.hutool.core.util.ArrayUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;

@Api(tags = "社交账号后台管理")
@RestController
@RequestMapping("/manage/social")
@AllArgsConstructor
public class SocialController {

	private final SocialInfoService socialInfoService;

	@ApiOperation("创建和修改")
	@PostMapping("/save")
	public CommonResult saveSocialInfo(@RequestBody SocialInfo socialInfo) {
		socialInfo.setPageType(4);
		return socialInfoService.saveOrUpdate(socialInfo) ? CommonResult.success() : CommonResult.failed("保存失败");
	}

	@ApiOperation("删除")
	@DeleteMapping("/delete/{id}")
	public CommonResult deleteSocialInfo(@PathVariable("id") String id) {
		return socialInfoService.removeById(id) ? CommonResult.success() : CommonResult.failed("删除失败");
	}

	@ApiOperation("批量删除")
	@DeleteMapping("/delete/{ids}")
	public CommonResult batchDeleteSocialInfo(@PathVariable("ids") String[] ids) {
		if (ArrayUtil.isNotEmpty(ids)) {
			for (String id : ids) {
				deleteSocialInfo(id);
			}
		}
		return CommonResult.success();
	}

	@ApiOperation("根据ID查询社交账号")
	@GetMapping("/getone")
	public CommonResult<SocialInfo> getSocialInfo(@RequestParam("id") String id) {
		return CommonResult.success(socialInfoService.getById(id));
	}

	@ApiOperation("查询当前法人下的所有社交账号")
	@GetMapping("/list/{legalCode}")
	public CommonResult<List<SocialInfo>> listSocialInfo(@PathVariable("legalCode") String legalCode) {
		return CommonResult.success(
				socialInfoService.list(Wrappers.lambdaQuery(SocialInfo.class).eq(SocialInfo::getLegalCode, legalCode)));
	}
}
